#!/bin/bash
cygwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
esac

# resolve links - $0 may be a softlink
THIS="$0"
while [ -h "$THIS" ]; do
  ls=`ls -ld "$THIS"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    THIS="$link"
  else
    THIS=`dirname "$THIS"`/"$link"
  fi
done

IS_CORE=0
#check for -core option
if [ "$1" == "-core" ] ; then
  IS_CORE=1
  shift
fi

# get arguments
COMMAND=$1
shift

# some directories
THIS_DIR=`dirname "$THIS"`
webtin_HOME=`cd "$THIS_DIR/.." ; pwd`

# some Java parameters
webtin_JAVA_HOME=/home/phuongdo
if [ "$webtin_JAVA_HOME" != "" ]; then
  #echo "run java in $webtin_JAVA_HOME"
  JAVA_HOME=$webtin_JAVA_HOME
fi
  
if [ "$JAVA_HOME" = "" ]; then
  echo "Error: JAVA_HOME is not set."
  exit 1
fi

JAVA=$JAVA_HOME/bin/java
JAVA_HEAP_MAX=-Xmx1g 

# check envvars which might override default args
if [ "$webtin_HEAPSIZE" != "" ]; then
  #echo "run with heapsize $webtin_HEAPSIZE"
  JAVA_HEAP_MAX="-Xmx""$webtin_HEAPSIZE""m"
  #echo $JAVA_HEAP_MAX
fi

# CLASSPATH initially contains $webtin_CONF_DIR, or defaults to $webtin_HOME/conf
CLASSPATH=${webtin_CONF_DIR:=$webtin_HOME/conf}
CLASSPATH=${CLASSPATH}:$JAVA_HOME/lib/tools.jar

# so that filenames w/ spaces are handled correctly in loops below
IFS=

if [ $IS_CORE == 0 ] 
then
  for f in $webtin_HOME/build/webtin-*.job; do
    CLASSPATH=${CLASSPATH}:$f;
  done

  # for releases, add Nutch job to CLASSPATH
  for f in $webtin_HOME/webtin-*.jar; do
    CLASSPATH=${CLASSPATH}:$f;
  done
else
  CLASSPATH=${CLASSPATH}:$webtin_HOME/build/classes
fi

# add libs to CLASSPATH
for f in $webtin_HOME/lib/*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done

for f in $webtin_HOME/lib/jetty-ext/*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done

# cygwin path translation
if $cygwin; then
  CLASSPATH=`cygpath -p -w "$CLASSPATH"`
fi

# setup 'java.library.path' for native-hadoop code if necessary
JAVA_LIBRARY_PATH=''
if [ -d "${webtin_HOME}/build/native" -o -d "${webtin_HOME}/lib/native" ]; then
  JAVA_PLATFORM=`CLASSPATH=${CLASSPATH} ${JAVA} org.apache.hadoop.util.PlatformName | sed -e 's/ /_/g'`
  
  if [ -d "$webtin_HOME/build/native" ]; then
    JAVA_LIBRARY_PATH=${HADOOP_HOME}/build/native/${JAVA_PLATFORM}/lib
  fi
  
  if [ -d "${webtin_HOME}/lib/native" ]; then
    if [ "x$JAVA_LIBRARY_PATH" != "x" ]; then
      JAVA_LIBRARY_PATH=${JAVA_LIBRARY_PATH}:${webtin_HOME}/lib/native/${JAVA_PLATFORM}
    else
      JAVA_LIBRARY_PATH=${webtin_HOME}/lib/native/${JAVA_PLATFORM}
    fi
  fi
fi

if [ $cygwin = true -a "X${JAVA_LIBRARY_PATH}" != "X" ]; then
  JAVA_LIBRARY_PATH=`cygpath -p -w "$JAVA_LIBRARY_PATH"`
fi

# restore ordinary behaviour
unset IFS

# default log directory & file
if [ "$webtin_LOG_DIR" = "" ]; then
  webtin_LOG_DIR="$webtin_HOME/logs"
fi
if [ "$webtin_LOGFILE" = "" ]; then
  webtin_LOGFILE='hadoop.log'
fi

#Fix log path under cygwin
if $cygwin; then
  webtin_LOG_DIR=`cygpath -p -w "$webtin_LOG_DIR"`
fi

webtin_OPTS="$webtin_OPTS -Dhadoop.log.dir=$webtin_LOG_DIR -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"
webtin_OPTS="$webtin_OPTS -Dhadoop.log.file=$webtin_LOGFILE"

if [ "x$JAVA_LIBRARY_PATH" != "x" ]; then
  webtin_OPTS="$webtin_OPTS -Djava.library.path=$JAVA_LIBRARY_PATH"
fi


# figure out which class to run
if [ "$COMMAND" = "runCrawler" ] ; then
  CLASS=com.solt.prism.core.MainExecutor
elif [ "$COMMAND" = "vbuilder" ] ; then
  CLASS=com.solt.prism.core.similar.VocabBuilder
else
  CLASS=$COMMAND
fi

# run it
exec java $JAVA_HEAP_MAX $webtin_OPTS -classpath "$CLASSPATH" $CLASS "$@"
